<template>
  <div class="collect-article">
    <van-button
      :icon="value ? 'star' : 'star-o'"
      :class="{ collected: value }"
      @click="onCollect"
      :loading="loading"
    />
  </div>
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article';
export default {
  name: 'CollectArticle',
  data() {
    return {
      loading: false,
    };
  },
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    acticleId: {
      type: [Number, Object, String],
      required: true,
    },
  },
  methods: {
    async onCollect() {
      this.loading = true;
      try {
        if (this.value) {
          await deleteCollect(this.acticleId);
        } else {
          await addCollect(this.acticleId);
        }
        this.$emit('input', !this.value);
        this.$toast.success(!this.value ? '收藏成功' : '取消成功');
      } catch (err) {
        this.$toast.fail('操作失败，请重试！');
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="less" scoped>
.collected {
  .van-icon {
    color: #ffa500;
  }
}
</style>
